<?php

class Admin_CourseController extends ExtLb_Controller_Action{

	private $courseTbl;
	private $managerTbl;
	private $courseFrm;
	
	//
	// === CONTROLLER NAY CHI CHO ADMIN DUNG
    public function init()
    {
        $this->loadCss('admin', 'course');
        $this->loadLayout('admin', 'layout');
        
        $this->courseTbl	= new Admin_Model_Monhoc();
        $this->managerTbl	= new Admin_Model_Manages();
        $this->courseFrm	= new Admin_Form_CourseForm();
        
    }

    public function indexAction(){
    	$courseSelect = $this->courseTbl->selectCourse();
    	    			
	    // xu ly tim kiem
    	if(isset($_GET['search_box_sumit'])){
    		if(empty($_GET['search_box_text'])){
    			$this->jShowMessage("Hãy nhập vào tên môn học cần tìm!!!");
    		}else{
    			$courseSelect = $this->courseTbl->selectCourse("
    					tenMonHoc like '%".$_GET['search_box_text']."%'"
    			);
    		}
    	}
    	
    	//
    	// === Paginator
    	$paginatorCourse = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($courseSelect));
    	$paginatorCourse->setItemCountPerPage(20)
    					->setPageRange(3)
    					->setCurrentPageNumber($this->_getParam('page','1'));
    					
    	//
    	$this->view->paginatorCourse = $paginatorCourse;
    	
    	//
    	// === Edit inForm
    	if(isset($_GET['edit']) && !empty($_GET['id'])){
    		$this->courseFrm->addAction('edit');
    		$this->courseFrm->addSubmitButton('cmdEdit', 'Lưu');
    		
    		$couse = $this->courseTbl->fetchRowMonHoc($_GET['id']);
    		
    		$this->courseFrm->addHiddenId('txtId', $couse['id']);
    		
    		$this->view->titleForm = "Thay đổi thông tin môn học";
    		$this->courseFrm->getElement('txtMonHoc')->setValue($couse['tenMonHoc']);
    		
    	}else{
    		$this->courseFrm->addAction('add');
    		$this->courseFrm->addSubmitButton('cmdAdd', 'Thêm');
    		
    		$this->view->titleForm	= "Thêm môn học";
    	}
    	
    	
    	$this->view->courseForm = $this->courseFrm;
    	
    	//
    	// === Forward when delete
    	if (isset($_GET['del']) && isset($_GET['course-check']) && !empty($_GET['course-check'])){
    		$this->_forward('delete','course','admin');
    	}
    }
    
    public function addAction(){
    	if (isset($_POST['cmdAdd'])){
    		if($this->courseFrm->isValid($this->_request->getPost())){
    			$monHoc = $this->_getParam('txtMonHoc');
    			
    			$this->courseTbl->addMonHoc($monHoc);
    			
    			$this->_request->clearParams();
    			$this->jShowMessage("Thành công!!!");
    			$this->_forward('index','course','admin');
    		}else{
    			$this->_redirect('/admin/course');
    		}
    	}else{
    		$this->_redirect('/admin/course');
    	}
    }
    
    public function editAction(){
    	if(isset($_POST['cmdEdit'])){
    		if($this->courseFrm->isValid($this->_request->getPost())){
    			$monHoc = $this->_getParam('txtMonHoc');
    			$id = $this->_getParam('txtId');
    			
    			$this->courseTbl->updateMonHoc($monHoc, $id);
    			
    			$this->jShowMessage("Thành công!!!");
    			$this->_forward('index','course','admin');
    		}else{
	    		$this->_redirect('/admin/course');
	    	}
    	}else{
    		$this->_redirect('/admin/course');
    	}
    }
    
    public function deleteAction(){
    	// Del one
    	if (isset($_GET['delete']) && isset($_GET['id']) && !empty($_GET['id'])){
    		$this->courseTbl->deleteMonHoc($_GET['id']);
    		$this->jShowMessage("Đã xóa!!!");
    	}else{
    		$this->_forward('index','course','admin');
    	}
    	
    	// === Del many
    	if (isset($_GET['del']) && isset($_GET['course-check'])){
    		$arrDel = $_GET['course-check'];
    		for ($i =0 ; $i <count($arrDel); $i++){
    			$this->courseTbl->deleteMonHoc($arrDel[$i]);
    		}
    	}else{
    		$this->_redirect('/admin/course/');	
    	}
    	
    	$this->_redirect('/admin/course/');
    }
    


}



